package gemini.pollux.ui.client.page.content.marketingplan.level.widget;

import gemini.basic.model.Level;
import gemini.pollux.ui.client.utils.widgets.dialog.GmnDialog;
import gemini.pollux.ui.client.utils.widgets.dialog.GmnDialogEvent;
import gemini.pollux.ui.client.utils.widgets.dialog.GmnDialogHandler;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.event.shared.HasHandlers;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;

public class LevelTableWidget extends Composite implements HasHandlers {

	public LevelTableWidget() {
		initTable();
		initHeader();
		initWidget(ftbSummary);
	}

	FlexTable ftbSummary;

	private void initTable() {
		ftbSummary = new FlexTable();
		ftbSummary.setWidth("100%");
		ColumnFormatter colFmt = ftbSummary.getColumnFormatter();

		colFmt.setWidth(0, "5%");
		colFmt.setWidth(1, "20%");
		colFmt.setWidth(2, "15%");
		colFmt.setWidth(3, "50%");
		colFmt.setWidth(4, "5%");
		colFmt.setWidth(5, "5%");
	}

	private void initHeader() {
		ftbSummary.setHTML(0, 0, "No");
		ftbSummary.setHTML(0, 1, "Name");
		ftbSummary.setHTML(0, 2, "Discount Scale");
		ftbSummary.setHTML(0, 3, "Description");
		ftbSummary.setHTML(0, 4, "&nbsp;");
		
		
		HTML htmAdd = new HTML("<div id=\"btnAdd\">" + "&nbsp;"
				+ "</div>");
		htmAdd.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				fireEvent(new LevelTableEvent());
			}
		});
		
		ftbSummary.setWidget(0, 5, htmAdd);
		
		ftbSummary.getRowFormatter().setStyleName(0, "tableHeader");
		ftbSummary.setStyleName("ftbSummary");
	}

	public void loadData(List<Level> items) {

		ftbSummary.removeAllRows();
		initHeader();

		if (items != null && !items.isEmpty()) {
			RowFormatter rowFmt = ftbSummary.getRowFormatter();
			int i = 1;
			for (int j = items.size() - 1; j >= 0; j--) {
				
				final Level it = items.get(j);
				
				if (i % 2 == 0) {
					rowFmt.setStyleName(i, "rowEven");
				}

				ftbSummary.setHTML(i, 0, String.valueOf(i));
				ftbSummary.setHTML(i, 1, it.getName());
				ftbSummary.setHTML(i, 2, String.valueOf(it.getDiscountScale()));
				ftbSummary.setHTML(i, 3, it.getDescription());

				HTML htmRemove = new HTML("<div id=\"btnDelete\">" + "&nbsp;"
						+ "</div>");
				htmRemove.addClickHandler(new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						confirmDelete(it);
					}
				});

				ftbSummary.setWidget(i, 5, htmRemove);
				
				HTML htmEdit = new HTML("<div id=\"btnEdit\">" + "&nbsp;"
						+ "</div>");
				htmEdit.addClickHandler(new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						fireEvent(new LevelTableEvent(LevelTableEvent.EventType.EDIT, it));
					}
				});
				ftbSummary.setWidget(i, 4, htmEdit);

				i++;
			}
		}
	}

	private void confirmDelete(final Level it) {
		String message = "Are you sure to delete this level?";

		GmnDialog dlgConfirm = new GmnDialog("Confirm", message, "Yes", "No");

		dlgConfirm.addConfirmationHandler(new GmnDialogHandler() {

			@Override
			public void onOK(GmnDialogEvent event) {
				fireEvent(new LevelTableEvent(LevelTableEvent.EventType.DELETE, it));
			}

			@Override
			public void onCancel(GmnDialogEvent event) {

			}
		});

		dlgConfirm.show();
	}

	public HandlerRegistration addLevelActionHandler(LevelTableHandler handler) {
		return addHandler(handler, LevelTableEvent.getType());
	}
}
